﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HerGoS.DAO;
using HerGoS.Interfaces;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace HerGoS.Administration.Engine
{
    public class GestorUpdate
    {

        public static void Procesar(string pathOrigen, string pathDestino)
        {
            try
            {
                ClearSql(pathDestino);
                CopyFile(pathOrigen, pathDestino);
                RunSql(pathDestino);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static void ClearSql(string pathDestino)
        {
            string pathSQL = Path.Combine(pathDestino, "sql");

            if (Directory.Exists(pathSQL))
                Directory.Delete(pathSQL, true);
        }

        static void RunSql(string pathDestino)
        {
            string pathSQL = Path.Combine(pathDestino, "sql");

            if (!Directory.Exists(pathSQL))
                return;

            DirectoryInfo sql = new DirectoryInfo(pathSQL);

            FileInfo[] files = sql.GetFiles();

            
            foreach (FileInfo file in files)
            {
                var script = File.ReadAllText(file.FullName);
                var conn = new SqlConnection(RepositoryFactory.GetConnection(Config.RepositoryName));
                Server server = new Server(new ServerConnection(conn));
                server.ConnectionContext.ExecuteNonQuery(script);
            }
        }

        static void CopyFile(string pathOrigen, string pathDestino)
        {

            if (!System.IO.File.Exists(pathOrigen) && !System.IO.Directory.Exists(pathOrigen))
            {
                throw new Exception("El objeto '" + pathOrigen + "' no existe o ha sido renombrado");
            }
            else
            {
                try
                {
                    DirectoryInfo source = new DirectoryInfo(pathOrigen);
                    DirectoryInfo destination = new DirectoryInfo(pathDestino);
                    CopyDirectory(source, destination);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
        {
            if (!destination.Exists)
            {
                destination.Create();
            }

            // Copy all files.
            FileInfo[] files = source.GetFiles();
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(destination.FullName, file.Name), true);
            }

            // Process subdirectories.
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destination.FullName, dir.Name);

                // Call CopyDirectory() recursively.
                CopyDirectory(dir, new DirectoryInfo(destinationDir));
            }
        }
    }
}
